<?php
class Zendvn_Model_Table_Service extends Zend_Db_Table{
	protected $_name = "service";
	protected $_primary = "id"; 
	protected $_arrParam = array();
	
	public $_searchFields = array('name'=>'- Question field');
	
	protected $_listSQL;
	public function statusItem($arrParam = null,$options = null){
		if($options == null){
			$status = ($arrParam['s'] == 'active' )? 'inactive':'active';				
			$data = array('status'=>$status);
			$where = 'id = ' . (int)$arrParam['id'];
			$this->update($data, $where);	
		}
		if($options['task'] == 'multi'){
			$status = $arrParam['s'];				
			if(count($arrParam['cid'])>0){
				if($status == 'active'){
					$ids = implode(',', $arrParam['cid']);
					$where = 'id IN (' . $ids . ')';
					$data = array('status'=>$status);
					$this->update($data, $where);
				}else{
					foreach ($arrParam['cid'] as $val){						
						$where = 'id = ' . $val;
						$data = array('status'=>$status);
						$this->update($data, $where);						
					}
				}
			}
		}
	}
	
	public function featureItem($arrParam = null,$options = null){
		if($options == null){
			$status = ($arrParam['s'] == 'active' )? 'inactive':'active';
			$data = array('feature'=>$status);
			$where = 'id = ' . (int)$arrParam['id'];
			$this->update($data, $where);
		}
		if($options['task'] == 'multi'){
			$status = $arrParam['s'];
			if(count($arrParam['cid'])>0){
				if($status == 'active'){
					$ids = implode(',', $arrParam['cid']);
					$where = 'id IN (' . $ids . ')';
					$data = array('feature'=>$status);
					$this->update($data, $where);
				}else{
					foreach ($arrParam['cid'] as $val){
						$where = 'id = ' . $val;
						$data = array('feature'=>$status);
						$this->update($data, $where);
					}
				}
			}
		}
	}
	
	public function deleteItem($arrParam = null,$options = null){
		if($options == null){
			if((int)$arrParam['id']>0){
				$where = 'id = ' . (int)$arrParam['id'];
				$this->delete($where);
			}
		}
		if($options['task'] == 'multi'){		
			if(count($arrParam['cid'])>0){	
				foreach ($arrParam['cid'] as $val){
					$where = 'id = ' . (int)$val;
					$this->delete($where);
				}
			}
		}
	}
	public function getItem($arrParam = null,$options = null){
		
		if($options == null){
			$where = 'id = ' . (int)$arrParam['id'];
			$result = $this->fetchRow($where);
			if(!empty($result))
				$result = $result->toArray();
		}		
		return $result;
	}
	
	public function listItem($arrParam = null,$options = null){
		if($options == null){
			$db = $this->getAdapter();
			$select = $db->select()
						 ->from(array('i' => $this->_name));
			$arrFilter = $arrParam['arrFilter'];
			
			if(!empty($arrFilter['col'])){
				$select->order($arrFilter['col'] . ' ' . $arrFilter['order']);			
			}
	
			if(!empty($arrFilter['status']) && $arrFilter['status'] != 'none'){
				$select->where('i.status = ?',$arrFilter['status']);
			}
			
			if(!empty($arrFilter['feature']) && $arrFilter['feature'] != 'none'){
				$select->where('i.feature = ?',$arrFilter['feature']);
			}
			
			if(!empty($arrFilter['keyword']) && !empty($arrFilter['searchField'])){
				$keyword = '%' . $arrFilter['keyword'] . '%';
				$select->where($arrFilter['searchField'] . ' LIKE ? ', $keyword);
			}
			
			$this->_listSQL = $select;
			$panigator = $arrParam['panigator'];	
			$select->limitPage($panigator['page'], $panigator['rowCount']);
			
			$result = $db->fetchAll($select);		 
		}
		
		if($options['task'] == 'feature-service'){
			$db = $this->getAdapter();
			$select = $db->select()
						 ->from(array('i' => $this->_name), array('id', 'name', 'description', 'picture'))
						 ->where('i.status = "active" AND i.feature = "active"')
						 ->order('i.id ASC')
						 ->limitPage(0, 5);
			$result = $db->fetchAll($select);			
		}
		
		if($options['task'] == 'right-service'){
			$db = $this->getAdapter();
			$select = $db->select()
						->from(array('i' => $this->_name), array('id', 'name', 'description', 'picture'))
						->where('i.status = "active"')
						->order('rand()')
						->limitPage(0, 5);
			$result = $db->fetchAll($select);
		}
		
		if($options['task'] == 'list-item'){
			$db = $this->getAdapter();
			$select = $db->select()
							->from(array('i' => $this->_name))
							->where('i.status = "active"')
							->order('i.id DESC');
			$this->_listSQL = $select;
			$panigator = $arrParam['panigator'];
			$select->limitPage($panigator['page'], $panigator['rowCount']);
			$result = $db->fetchAll($select);
		}
		
		if($options['task'] == 'search-item'){
			$db = $this->getAdapter();
			$select = $db->select()
						->from(array('i' => $this->_name))
						->columns(new Zend_Db_Expr("'service' AS data"))
						->where('i.name like "%' . $arrParam['keyword'] . '%"')
						->orWhere('i.content like "%' . $arrParam['keyword'] . '%"')
						->order('i.id DESC');
			$result = $db->fetchAll($select);
		}
		return $result;
	}
	
	public function totalItem($arrParam = null,$options = null){
		
		if($options == null){
			$db = $this->getAdapter();
			$select = $this->_listSQL->reset(Zend_Db_Select::COLUMNS);
			$select = $this->_listSQL->reset(Zend_Db_Select::LIMIT_COUNT);
			$select = $this->_listSQL->reset(Zend_Db_Select::LIMIT_OFFSET);
			$select = $this->_listSQL->reset(Zend_Db_Select::ORDER);
			$select = $this->_listSQL->reset(Zend_Db_Select::GROUP);
			$select->columns('COUNT(i.id) AS total');	
			$result = $db->fetchOne($select);
		}
		return $result;
		
	}
	
	public function saveItem($arrParam = null,$options = null){
		
		$function  = Zendvn_System_Function::call($options['task']);
		$this->$function($arrParam);
	}
	
	public function adminAdd($arrParam = null){
		$info = new Zendvn_System_Info();
		$idUser = $info->getMemberInfo('id');
		
		$row = $this->fetchNew();
		$row->name			= $arrParam['name'];			
		$row->picture		= $arrParam['picture'];
		$row->description	= $arrParam['description'];
		$row->content		= $arrParam['content'];
		$row->html_descript	= $arrParam['html_descript'];
		$row->html_keyword	= $arrParam['html_keyword'];
		$row->status		= ($arrParam['status']=='') ? 'inactive':$arrParam['status'];
		$row->feature		= $arrParam['feature'];
		$row->save();
			
	}
	
	public function adminEdit($arrParam = null){
		$info 				= new Zendvn_System_Info();
		$idUser 			= $info->getMemberInfo('id');
		
		$where  			= 'id = ' . (int)$arrParam['id'];
		$row 				= $this->fetchRow($where);
		$row->name			= $arrParam['name'];	
		$row->picture		= $arrParam['picture'];
		$row->description	= $arrParam['description'];
		$row->content		= $arrParam['content'];
		$row->html_descript	= $arrParam['html_descript'];
		$row->html_keyword	= $arrParam['html_keyword'];
		$row->status		= ($arrParam['status']=='') ? 'inactive':$arrParam['status'];
		$row->feature		= $arrParam['feature'];
		$row->save();
	}
}